﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using CCWin;
namespace MIDou
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        IniFiles ini = new IniFiles(Application.StartupPath + @"\Config.INI");
        int x, y, z;
        int index = 10;
        ArrayList arrayList = new ArrayList();
        private void xz1_Click(object sender, EventArgs e)
        {
            if (IsOK(xz1.Text))
            {
                OKForm ok = new OKForm();
                ok.ShowDialog();
            }
            else {
                NOForm no = new NOForm();
                no.ShowDialog();
            }
            Info();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DirectoryInfo folder = new DirectoryInfo(Application.StartupPath + @"\image\");

            foreach (FileInfo file in folder.GetFiles("*.jpg"))
            {
                arrayList.Add(file.FullName);
            }
            if (ini.ExistINIFile())
            {
                string Value = ini.IniReadValue("参数设置", "最大数值");

                index = Convert.ToInt32(Value);
            }
            Info();
        }

        private void xz2_Click(object sender, EventArgs e)
        {
            if (IsOK(xz2.Text))
            {
                OKForm ok = new OKForm();
                ok.ShowDialog();
            }
            else
            {
                NOForm no = new NOForm();
                no.ShowDialog();
            }
            Info();
        }

        private void xz3_Click(object sender, EventArgs e)
        {
            if (IsOK(xz3.Text))
            {
                OKForm ok = new OKForm();
                ok.ShowDialog();
            }
            else
            {
                NOForm no = new NOForm();
                no.ShowDialog();
            }
            Info();
        }

        private void xz4_Click(object sender, EventArgs e)
        {
            if (IsOK(xz4.Text))
            {
                OKForm ok = new OKForm();
                ok.ShowDialog();
            }
            else
            {
                NOForm no = new NOForm();
                no.ShowDialog();
            }
            Info();
        }

        public void Info() {
            fly1.Controls.Clear();
            fly2.Controls.Clear();
            //sz1.Text ="";
            //sz2.Text = "";
            //fh.Text = "";
            //xz1.Text = "";
            //xz2.Text = "";
            //xz3.Text = "";
            //xz4.Text = "";
            Random op = new Random();
            z = op.Next(0, 2);
            if (z == 1)//加法
            {
                fh.Text = "+";
                do
                {
                   Random n = new Random();
                    x = n.Next(1, index);
                    y = n.Next(1, index);
                } while (x + y > index);
                sz1.Text = x.ToString();
                sz2.Text = y.ToString();
                Random op1 = new Random();
                int z1 = op1.Next(1, 4);
                if (z1 == 1)
                {
                    xz1.Text = (Convert.ToInt32(sz1.Text) + Convert.ToInt32(sz2.Text)).ToString();
                    xz2.Text = op1.Next(1, index).ToString(); 
                    xz3.Text = op1.Next(1, index).ToString();
                    xz4.Text = op1.Next(1, index).ToString();

                }
                else if (z1 == 2)
                {
                    xz2.Text = (Convert.ToInt32(sz1.Text) + Convert.ToInt32(sz2.Text)).ToString();
                    xz1.Text = op1.Next(1, index).ToString();
                    xz3.Text = op1.Next(1, index).ToString();
                    xz4.Text = op1.Next(1, index).ToString();
                }
                else if (z1 == 3)
                {
                    xz3.Text = (Convert.ToInt32(sz1.Text) + Convert.ToInt32(sz2.Text)).ToString();
                    xz1.Text = op1.Next(1, index).ToString();
                    xz2.Text = op1.Next(1, index).ToString();
                    xz4.Text = op1.Next(1, index).ToString();
                }
                else {
                    xz4.Text = (Convert.ToInt32(sz1.Text) + Convert.ToInt32(sz2.Text)).ToString();
                    xz2.Text = op1.Next(1, index).ToString();
                    xz3.Text = op1.Next(1, index).ToString();
                    xz1.Text = op1.Next(1, index).ToString();
                }

            }
            else//减法
            {
                fh.Text = "-";
                do
                {
                    Random n = new Random();
                    x = n.Next(1, index);
                    y = n.Next(1, index);
                } while (x < y);
                sz1.Text = x.ToString();
                sz2.Text = y.ToString();
                if (x < y)
                {
                    sz1.Text = y.ToString();
                    sz2.Text = x.ToString();
                }
              
                Random op1 = new Random();
                int z1 = op1.Next(1, 4);
                if (z1 == 1)
                {
                    xz1.Text = (Convert.ToInt32(sz1.Text) - Convert.ToInt32(sz2.Text)).ToString();
                   
                    xz2.Text = op1.Next(1, index).ToString();
                    xz3.Text = op1.Next(1, index).ToString();
                    xz4.Text = op1.Next(1, index).ToString();
                }
                else if (z1 == 2)
                {
                    xz2.Text = (Convert.ToInt32(sz1.Text) - Convert.ToInt32(sz2.Text)).ToString();
                    xz1.Text = op1.Next(1, index).ToString();
                    xz3.Text = op1.Next(1, index).ToString();
                    xz4.Text = op1.Next(1, index).ToString();
                }
                else if (z1 == 3)
                {
                    xz3.Text = (Convert.ToInt32(sz1.Text) - Convert.ToInt32(sz2.Text)).ToString();
                    xz1.Text = op1.Next(1, index).ToString();
                    xz2.Text = op1.Next(1, index).ToString();
                    xz4.Text = op1.Next(1, index).ToString();
                }
                else
                {
                    xz4.Text = (Convert.ToInt32(sz1.Text) - Convert.ToInt32(sz2.Text)).ToString();
                    xz2.Text = op1.Next(1, index).ToString();
                    xz3.Text = op1.Next(1, index).ToString();
                    xz1.Text = op1.Next(1, index).ToString();
                }
            }
            int index1 = Convert.ToInt32(sz1.Text);
            for (int i = 0; i < index1; i++)
            {
                PictureBox pic = new PictureBox();
                int index = op.Next(0, arrayList.Count-1);
                pic.SizeMode = PictureBoxSizeMode.Zoom;
                pic.Width = 80;
                pic.Height = 80;
                pic.Image = Image.FromFile(arrayList[index].ToString());
                fly1.Controls.Add(pic);

            }
          
            int index2 = Convert.ToInt32(sz2.Text);
            for (int i = 0; i < index2; i++)
            {
                PictureBox pic = new PictureBox();
                int index = op.Next(0, arrayList.Count - 1);
                pic.Width = 80;
                pic.Height = 80;
                pic.SizeMode = PictureBoxSizeMode.Zoom;
                pic.Image = Image.FromFile(arrayList[index].ToString());
                fly2.Controls.Add(pic);

            }
        }
        public bool IsOK(string Value) {
            bool isok = false;
            string FH = fh.Text;
            int i = 0;
            if (FH == "+")
            {
                i = Convert.ToInt32(sz1.Text) + Convert.ToInt32(sz2.Text);
            }
            else {
                i = Convert.ToInt32(sz1.Text) - Convert.ToInt32(sz2.Text);
            }
            if (Value==i.ToString())
            {
                isok = true;
            }
            return isok;
        }




    }
}
